<?php
/**
 * User: hjd-yao
 * Date: 2019/4/16
 * Time: 13:29
 */

namespace App\Http\Controllers\Shop;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Shop\ShopCategoryModel;
use Illuminate\Http\Request;

class ShopCategoryController extends BackendController
{
    /**
     * @title 商品分类
     * @return View
     */
    public function categories()
    {
        $list = ShopCategoryModel::init()->getDTree(true);
        if(!$list){
            return redirect()->route('b_ShopCategory_create');
        }
        return view('backend::shop.category.categories',[
            'page_title'=>'商品分类',
            'treeList'=>json_encode($list)
        ]);
    }

    /**
     * @title 添加商品分类
     * @param Request $request
     * @return SelfResponse|View
     */
    public function create(Request $request)
    {
        if($request->isMethod('post')){
            $category = new ShopCategoryModel();
            $data = $request->post();
            if(!$category->add($data)){
                return SelfResponse::error('分类添加失败');
            }
            return SelfResponse::success('分类添加成功','b_ShopCategory_categories');
        }else{
            $parent_info = ShopCategoryModel::init()->getTrees();
            return view('backend::shop.category.add',[
                'page_title'=>'添加分类',
                'parent_info'=>json_encode($parent_info),
            ]);
        }

    }

    /**
     * @title 编辑商品分类
     * @param Request $request
     * @param $id
     * @return SelfResponse|View
     */
    public function update(Request $request,$id)
    {
        if($request->isMethod('post')){
            $category = new ShopCategoryModel();
            $data = $request->post();
            if(!$category->edit($id,$data)){
                return SelfResponse::error($category->getErrorMsg('分类编辑失败'));
            }
            return SelfResponse::success('分类编辑成功','b_ShopCategory_categories');
        }else{
            $info = ShopCategoryModel::find($id);
            $parent_info = ShopCategoryModel::init()->getTrees();
            return view('backend::shop.category.edit',[
                'page_title'=>'编辑分类',
                'info'=>$info,
                'parent_info'=>json_encode($parent_info),
            ]);
        }
    }

    /**
     * @title 删除商品分类
     * @param $id
     * @return SelfResponse
     */
    public function delete($id)
    {
        $info = ShopCategoryModel::find($id);
        if(!$info){
            return SelfResponse::error('删除的分类信息不存在');
        }
        if($info->delete()){
            ShopCategoryModel::whereRaw("FIND_IN_SET(?,`parent_ids`)",[$id])->delete();
            return SelfResponse::success('删除成功','b_ShopCategory_categories');
        }
        return SelfResponse::error('删除失败');
    }

    /**
     * @title 启用/禁用分类
     * @param $id
     * @return SelfResponse
     */
    public function switchCategory($id)
    {
        $info = ShopCategoryModel::find($id);
        if(!$info){
            return SelfResponse::error('删除的分类信息不存在');
        }
        $info->status = abs($info->status-1);
        if(!$info->save()){
            return SelfResponse::error('操作失败');
        }
        return SelfResponse::success('操作成功');
    }
}